<?php
/**
 * @author      :   PhuongTN
 * @name        :   server-log-error.php
 * @version     :   2012
 * @copyright   :   FPT Online
 * @todo        :   Cronjob run everyday: update statistic
 */
//Load staring
echo "Staring at " . date('d/m/Y H:i:s', time()) . "\n";

//Define data
define('DOCUMENT_ROOT', realpath(dirname(__FILE__)));
define('APPLICATION_PATH', realpath(DOCUMENT_ROOT . '/../../application'));
define('LIBS_PATH', realpath(DOCUMENT_ROOT . '/../../library'));
define('SERVER_PATH', realpath(DOCUMENT_ROOT . '/../../server/Job/models'));
define('ROOT_PATH', realpath(DOCUMENT_ROOT . '/../../'));
//Setup Include Paths
set_include_path(implode(PATH_SEPARATOR, array(
            LIBS_PATH,
            SERVER_PATH,
            APPLICATION_PATH.'/modules/default/models',
            get_include_path()
        )));
//Load Autoloader
require_once 'Zend/Loader/Autoloader.php';

Zend_Loader_Autoloader::getInstance()->setFallbackAutoloader(true);

//Check console params options
$opts = new Zend_Console_Getopt(
            array(
                'v-i' => 'verbose option'
            ));

//Get info console
$verbose = $opts->getOption('v');

//load application env by unix var
define('APP_ENV', isset($_SERVER['APPLICATION_ENV']) ? $_SERVER['APPLICATION_ENV'] : 'development');
define('ZONE_ENV', isset($_SERVER['ZONE_ENV']) ? $_SERVER['ZONE_ENV'] : 'hcm');


//Print information envirement
if ($verbose) {
    echo "DOCUMENT_ROOT : " . DOCUMENT_ROOT . "\n";
    echo "APPLICATION_PATH : " . APPLICATION_PATH . "\n";
    echo "LIBS_PATH : " . LIBS_PATH . "\n";
    echo "ENVIRONMENT : " . APP_ENV . "\n";
}

//Load defined configuration
require_once APPLICATION_PATH . '/configs/defined.php';

//Init logger
if ($verbose) {
    $logfile = DOCUMENT_ROOT . '/logs/sync-statistic-' . date("Y-m-d") . '.log';
    $logger = new Zend_Log();
    $writer = new Zend_Log_Writer_Stream($logfile);
    $logger->addWriter($writer);
}
//Try execute
$result = 0;
try {
    
    $model = new JobAsync();
    $result = $model->syncCacheStatistic(array());
    
    //Try close
    Fpt_Global::closeAllDBInstance();
    Fpt_Global::closeAllCacheServer();
    Fpt_Global::closeAllRedisServer();
    //Debug

    if ($verbose) {
        $logdata = "\n";
        $logdata .= "Result execute :" . Zend_Json::encode($result);
        $logdata .= "\n";
        $logger->log($logdata, Zend_Log::INFO);

    }
} catch (Exception $ex) {
    $logdata = "Error :" . $ex->getMessage();
    $logger->log($logdata, Zend_Log::ERR);
}


